<template>
  <div class="myContailer">
    <el-row class="myTitleBorderBottom">
      <el-col :span="12" class="myLineHeight_40px">{{this.title}}</el-col>
      <el-col :span="12" class="myTextAlignRight">
        <el-button type="primary" class="myBgGreen" @click="addArea" v-if="isShow(110)">新 增</el-button>
        <!-- <el-button type="primary" class="myBgGreen">导出Excel</el-button> -->
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px" :gutter="10">
      <el-col :span="4">
        <el-select 
          v-model="district_selected"
          clearable
          placeholder="请选择片区" >
          <el-option
            v-for="item in district_list"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-col>
      <el-col :span="4">
        <el-input placeholder="请输入关键词" v-model="kw"></el-input>
      </el-col>
      <el-col :span="3">
        <el-button type="primary" class="myBgGreen" @click="fuzzySearch">查 询</el-button>
      </el-col>
    </el-row>
    <el-row class="myMarginTop_10px">
      <el-col :span="24">
        <el-table
          v-loading="loading"
          ref="multipleTable"
          :data="list"
          :border = true
          :stripe = true
          style="width: 100%"
          @selection-change="handleSelectionChange">
          <el-table-column
            type="selection"
            width="55">
          </el-table-column>
          <el-table-column
            type="index"
            label="序号"
            :index="indexMethod"
            width="100"
            >
          </el-table-column>
          <el-table-column
            prop="district_name"
            label="所属片区"
            >
          </el-table-column>
          <el-table-column
            prop="area_name"
            label="小区名称"
            >
          </el-table-column>
          <el-table-column
            prop="area_no"
            label="小区编号"
            >
          </el-table-column>
          <el-table-column
            prop="area_address"
            label="地址"
            >
          </el-table-column>
          <el-table-column
            prop="floor_space"
            label="占地面积"
            >
          </el-table-column>
          <el-table-column
            prop="total_floor_space"
            label="总建筑面积"
            >
          </el-table-column>
          <el-table-column
            prop="greening_ratio"
            label="绿化率"
            >
          </el-table-column>
          <el-table-column
            label="操作"
            width="100">
            <template scope="scope">
              <el-button class="look" type="text" size="small" @click="getDetail(scope.row.area_id)">查看</el-button> |
              <el-button type="text primary" class="look" size="small" @click="modify(scope.row.area_id)" v-if="isShow(111)">编辑</el-button> 
            </template>
          </el-table-column>
        </el-table>
        <div class="block">                    
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="page_num"
            :page-sizes="[10, 15]"
            :page-size="page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
          </el-pagination>
        </div>
      </el-col>
    </el-row>

    <!-- 确定删除弹框开始 -->
    <el-dialog title="确定删除?" class="myRoleDialog" :visible.sync="deleteDialog">
      <div>是否要删除编号为{{lookObj.area_name}}({{lookObj.area_no}})的信息！</div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" class="myBgGreen" @click="deleteDialog = false">取 消</el-button>
        <el-button type="primary" class="myBgGreen" @click="sureDelete">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 确定删除弹框结束 -->
  
  </div>

</template>

<style scoped>
  .myContailer{
    background: #fff;
    padding: 10px;
  }
  .myTitleBorderBottom{
    color: #000;
    font-weight: 500;
    font-size: 16px;
    border-bottom: 1px solid #dbdbdb;
    padding-bottom: 5px;
  }
  .myBgGreen{
    background: #00c2a9;
    border-color: #00c2a9;
    color: #fff;
    margin-top:2px;
  }
  .myBgGreen:hover{
    background: #33ceba;
  }
  .myTextAlignRight{
    text-align: right;
  }
  .myLineHeight_40px{
    line-height: 40px;
  }
  .myMarginTop_10px{
    margin-top: 10px;
  }
</style>

<script>
  import { getAreaList , getDistrictList } from '@/api/area';
  export default {
    data(){
      return{
        title:"小区管理",
        list:[],
        total:null, //总条数  
        page_num: null, //当前页码
        page_size: null, //每页显示多少条
        id:null,
        index:null,
        idArr:[],
        lookObj:[],
        loading:false,
        multipleSelection: [],
        kw:"",
        deleteDialog:false,
        district_list:[],
        district_selected:""
      }
    },
    mounted(){
      document.title=this.title;
      this.onGetDistrictList();//拉取片区
      this.fetchAreaList();
    },
    methods:{
      fetchAreaList() {
        let vm = this;
        let params = {
          district_id:vm.district_selected,
          kw: vm.kw,
          page_num: vm.page_num,
          page_size: vm.page_size
        }; 
        vm.loading=true;
        getAreaList(params)
        .then(response => {
          let res = response.data;
          vm.list = res.rows;
          vm.total = res.total;
          vm.page_num = res.page_num;
          vm.page_size = res.page_size;
          vm.loading=false;
          // console.log(vm.list)
        }).catch(error => {
          vm.loading=false;
        })
      },

      fuzzySearch:function(){
        this.page_num=1;
        this.fetchAreaList();
      },
      
      handleSizeChange(val) {//每页条数控制
        this.page_size = val;
        this.fetchAreaList();
      },

      handleCurrentChange(val) {//每页
        let vm=this;
        let xqglConfig=window.sessionStorage.getItem("xqglConfig");
        if(xqglConfig){
          let config=JSON.parse(xqglConfig);
          vm.district_selected=config.district_selected;
          vm.kw=config.kw;
          vm.page_num=config.page_num;
          window.sessionStorage.removeItem("xqglConfig");
        }else{
          vm.page_num = val;
        }
        vm.fetchAreaList();
      },

      addArea:function(){
        this.$router.push({
          path:'/xqglComonents',
          query: {type: 'add', pageNum: this.page_num}
        })
      },

      getDetail:function(areaId){
        let vm=this;
        let xqglConfig={
          district_selected:vm.district_selected,
          kw:vm.kw,
          page_num:vm.page_num
        }
        window.sessionStorage.setItem("xqglConfig",JSON.stringify(xqglConfig));
        vm.$router.push({
          path:'/xqglComonents',
          query: {type: 'info', id: areaId}
        })
      },

      modify:function(areaId){
        let vm=this;
        let xqglConfig={
          district_selected:vm.district_selected,
          kw:vm.kw,
          page_num:vm.page_num
        }
        window.sessionStorage.setItem("xqglConfig",JSON.stringify(xqglConfig));
        vm.$router.push({
          path:'/xqglComonents',
          query: {type: 'update', id: areaId}
        })
      },

      deleteData:function (datas,index) {
        this.idArr.push(datas.area_id);
        this.index = index;
        this.lookObj = datas
      },

      sureDelete:function(){
        var pageArr = {
          page_size : this.page_size,
          page_num : this.page_num,
        }
        if(this.list.length==1){
          var pageArr = {
            page_size : this.page_size,
            page_num : this.page_num - 1
          }
        }
        var arrId = {
          area_ids : this.idArr.join(",")
        }
        //optVue.deleteXqData(this,this.index,Tips,pageArr,arrId);
        this.deleteDialog = false;
      },
     
      allDelete:function(event){
        if(this.multipleSelection!=0){
          var pageArr = {
            page_num : this.page_num - 1
          }
          
          var arrId = {
            area_ids : this.idArr.join(",")
          }
          // console.log(JSON.stringify(arrId)) 
          //optVue.deleteXqData(this,this.index,Tips,pageArr,arrId);
          
          this.idArr = [];
        }
        else{
          // Tips.init({
          //   type:"fail",
          //   message:"请选择要删的项",
          //   class:"on"
          // });
        }
      },

      
      handleSelectionChange(val) {
        this.multipleSelection = val;
        if(val.length!=0){
          for(var i = 0;i<val.length;i++){
            this.idArr.push(val[i].area_id)
          }
        }else{
          this.idArr = [];
        }
      },

      indexMethod(index) {
          return (this.page_num-1)*this.page_size + index + 1
      },
      onGetDistrictList(){
        let vm=this;
        getDistrictList()
        .then(response => {
          vm.district_list=response.data;
        })
        .catch(error => {
          // console.log("拉取失败",error)
        })
      }
    }
  }
</script>



